import axios from "axios"
import qs from "qs"

//1.请求拦截
axios.interceptors.request.use(config => {
    //逻辑
    return config;
})

// 2.响应拦截
axios.interceptors.response.use(res => {
    //统一打印
    console.log('url:' + res.config.url);
    console.log(res);

    //失败
    if (res.data.code !== 200) {
        alert(res.data.msg,1)
    }

    return res;
})

// 3.get 
export const get = (url, params = {}) => {
    return axios({
        url,
        params
    })
}

// 4.post 
export const post = (url, params, isFile) => {
    let data = null;
    if (isFile) {
        data = new FormData()
        for (let key in params) {
            data.append(key, params[key])
        }
    } else {
        data = qs.stringify(params)
    }

    return axios({
        url,
        method: "post",
        data
    })
}